package main;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class XMLHandler {

	
	private XMLHandler xmlHandler;
	private static File xmlSource;
	private static Document doc;
	
	public static ArrayList<MusicFile> parseXMLToMusicFileList(File xmlSource) throws ParserConfigurationException, SAXException, IOException {
	
		
		XMLHandler.xmlSource = xmlSource;
		
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		
		if (!xmlSource.exists()) {
			doc = dBuilder.newDocument();
			return new ArrayList<MusicFile>();
		}
		doc = dBuilder.parse(xmlSource);
		
		NodeList t = doc.getElementsByTagName("file");
		
		ArrayList<MusicFile> returnList = new ArrayList<MusicFile>();
		
		for(int tel = 0  ; tel < t.getLength(); tel++) {
			Element temp =  (Element) t.item(tel);
			
			int residentComputer  = Integer.valueOf(getTagValue("residentComputer", temp));
			String tempDate = getTagValue("airDate", temp);
			String[] date = tempDate.split("/"); 
			Calendar airCalendar = Calendar.getInstance();
			airCalendar.clear();
			airCalendar.set(Integer.valueOf(date[2]),Integer.valueOf(date[1]),Integer.valueOf(date[0]));
			String name = getTagValue("name", temp);
			String  path = getTagValue("path", temp);
			String type = getTagValue("musicType", temp);	
			boolean uploaded = getTagValue("uploaded", temp).equalsIgnoreCase("true") ? true : false;
			MusicType musicType = MusicType.getType(type);
			File referenceFile = new File(path + "\\" + name + "." + type); 
		 
			MusicFile tempMusicFile = new  MusicFile(residentComputer, airCalendar, referenceFile, uploaded, musicType, name, path);
			returnList.add(tempMusicFile);
		}
		return returnList;
	}
	
	 private static String getTagValue(String sTag, Element eElement) {
			NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
            Node nValue = (Node) nlList.item(0);

			return nValue.getNodeValue();
		  }
	
	
	 public static void addMusicFile(MusicFile musicFile) throws TransformerException {
		 
		NodeList root = doc.getElementsByTagName("musicFiles");
		
		
		Element file = doc.createElement("file");
		Element  name =  doc.createElement("name");
		name.setTextContent(musicFile.name);
		file.appendChild(name);
		
		Element airDate =  doc.createElement("airDate");
		airDate.setTextContent("" + musicFile.airDate.get(Calendar.DAY_OF_MONTH) + "/" 
		+ musicFile.airDate.get(Calendar.MONTH) + "/" +    musicFile.airDate.get(Calendar.YEAR));
		file.appendChild(airDate);
		
		Element uploaded =  doc.createElement("uploaded");
		uploaded.setTextContent(musicFile.uploaded ? "true" : "false");
		file.appendChild(uploaded);
		
		Element musicType=  doc.createElement("musicType");
		musicType.setTextContent(MusicType.getString(musicFile.getMusicType()));
		file.appendChild(musicType);
		 
		Element path=  doc.createElement("path");
		path.setTextContent(musicFile.getPath());
		file.appendChild(path);
 
		Element residentComputer=  doc.createElement("residentComputer");
		residentComputer.setTextContent(""+musicFile.residentComputer);
		file.appendChild(residentComputer);
		root.item(0).appendChild(file);
		
		 writeDocumentToXML() ;
		 
	 }
	
	 
	static void  writeDocumentToXML() throws TransformerException {
			
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(xmlSource);
			transformer.transform(source, result);
	 }
	 
	 static void update(String nameOfFile, String node , String value) throws TransformerException {
			NodeList t = doc.getElementsByTagName("file");
 
			for(int tel = 0  ; tel < t.getLength(); tel++) {
				Element temp =  (Element) t.item(tel);
				if (getTagValue("name", temp).compareToIgnoreCase(nameOfFile) == 0) {
					temp.getElementsByTagName(node).item(0).setTextContent(value);
					break;
				}
			}
			
		 writeDocumentToXML();
	 }
	 
	 
	 
}
